package com.yingli.network;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    private TextView tvWeb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//指定的xml
        initView();
        initListener();
        tvWeb = findViewById(R.id.tv_Web);//指定id

        Button btnRequest = findViewById(R.id.btn_httpclient);
        btnRequest.setOnClickListener(v -> { // 调用。。
            startActivity(new Intent(this,HttpClientActivity.class));
        });
        requestData();
    }

    private void initListener() {
    }

    private void initView() {
    }
    private Handler handler = new Handler(Looper.getMainLooper()){
        //处理发送的消息
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //此处为携带的数据
            String data = (String) msg.obj;
            tvWeb.setText(data);
        }
    };
        //网络请求：
    private void requestData() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                HttpURLConnection urlConnection = null;
                try {
                    URL url = new URL("https://api.art.youqu.life/article/list/\n");//需要捕获异
                    urlConnection = (HttpURLConnection) url.openConnection();//需要捕获异常
                    //设置请求的方式为GET
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setConnectTimeout(3000);
                    //设置请求的编码格式
                    urlConnection.setRequestProperty("Charset","UTF-8");
                    //设置请求文本类型为html格式的数据
                    urlConnection.setRequestProperty("Content-Type","text/html;charset=utf-8;");
                    urlConnection.connect();
                    //获取请求响应码
                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode == 200){
                        InputStream inputStream = urlConnection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer buffer = new StringBuffer();
                        while(true){
                            String temp;
                            if ((temp = reader.readLine()) !=null){
                                buffer.append(temp);
                            }else{
                                break;
                            }
                        }
                        //方式一 通过Handler更新UI
//                        Message message = Message.obtain();
//                        message.obj = buffer.toString();
//                        handler.sendMessage(message);
                        //更新UI
                        runOnUiThread(() ->{
                            tvWeb.setText(buffer.toString());
                        });
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    if (urlConnection != null){
                        urlConnection.disconnect();
                    }
                }
            }
        }.start();
    }
}